import { finance } from "../../../../js/modules/store.js";
import { theme } from "../../../../js/modules/theme.js";
import { api,util } from '../../../../js/util';
import authorize from '../../../../js/modules/authorize.js'
import BasePage from '../../../../js/common/base-page'
BasePage({
  pageKey:"integral",

  /**
   * 页面的初始数据
   */
  data: {
    integral:0,
    page: {
      PageIndex: 1,
      PageSize: 20,
      keywords: "",
      sortField: "AddTime",
      sortDirect: "desc"
    },
    list: [],
    hasMoreData: true,
    gradeList: [
      { num: 3, name: '1天', active: true },
      { num: 6, name: '2天', active: true },
      { num: 10, name: '3天', active: true },
      { num: 20, name: '4天', active: true },
      { num: 30, name: '5天', active: false },
      { num: 40, name: '6天', active: false },
      { num: 80, name: '7天', active: false }
    ],
    showShadow: false,
    showRule: false,
    checkInInfo:{},
    headerBg:""
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
   
  },

  /**加载商品列表 */
  loadList() {
    var that = this;
    var page = this.data.page;
    finance.loadIntegralList(page, function (content) {
      var hasMoreData = content.count > (page.PageIndex) * page.PageSize;
      var index = that.data.list.length;
      var data = content.data;
      //当分页大于1时，累加
      if (page.PageIndex > 1) {
        data = that.data.list.concat(data);
      }
      //如果key为变量作为key值的时候要用[]包裹起来
      that.setData({
        list: data,
        hasMoreData: hasMoreData
      });
    })
  },
  /**
* 页面相关事件处理函数--监听用户下拉动作
*/
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.data.page.PageIndex = 1;
    this.loadList();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    if (this.data.hasMoreData) {
      var index = this.data.page.PageIndex;
      this.data.page.PageIndex = index + 1;
      this.loadList();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      headerBg: api.titleColor||"#e56b14"
    })
    this.checkInIntegral();
    
  },
  /**
   * 签到
   */
  checkInIntegral(){
    var that=this;
    finance.checkInIntegral(function (res){
      if(res.Success){
        that.setData({
          showShadow: true,
          checkInInfo: res.Content
        });
      }
      else{
        that.setData({
          showShadow: false
        });
      }
      //获取自身的积分
      finance.get_self_integral(function (res) {
        res = res.data;
        if (res.Success) {
          that.setData({
            integral: res.Content.Integral
          });
        }
      });
      that.data.page.PageIndex = 1;
      that.loadList();
      that.getCheckInInfo();
    });
  },
  /**
   * 获得签到信息
   */
  getCheckInInfo() {
    var that = this;
    finance.getCheckInInfo(function (res) {
      if (res.Success) {
        var content = res.Content;
        that.setData({
          checkInInfo: content
        });
      }
      else {
        that.setData({
          showShadow: false
        });
      }
    });
  },
  onSkipTo(e){
    let idx = e.currentTarget.dataset.idx;
    let current = this.data.list[idx];
    if (current==undefined) return;
    if (current.OddNumber && current.OddNumber.length>0){
      let fix = current.OddNumber.substring(0,4)
      if (fix == "XSDD"){
        return;
      }
      theme.routerTo("orderDetail", {
        oddNumber: current.OddNumber
      });
    }
   
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 显示规则弹层
   */
  showRuleShadow: function () {
    this.setData({
      showShadow: true,
      showRule: true
    })
  },
  /**
   * 关闭弹层
   */
  shutShadow: function () {
    this.setData({
      showShadow: false,
      showRule: false
    })
  },
  onUnload: function (e) {
    this.data.pre_page = this.getPreData();
    this.data.pre_page.data.isBack = true;
  },
  getPreData: function () {
    const pages = getCurrentPages();
    const length = pages.length;
    const prePage = pages[length - 2]; //上一页
    return prePage;
  }
})